// pages/login/login.js
const md5 = require('./md5.js');
const util = require('../../utils/util.js');
const wxPromisify = util.wxPromisify;
const wx_request_promisify = wxPromisify(wx.request);
let prefix = 'https://gr.tunnel.qydev.com/6ds';
Page({
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  userInfo(res) {
     if(res.currentTarget.id === 'user_name'){
       this.setData({phonenumber : res.detail.value})
     }else if(res.currentTarget.id === 'password'){
       this.setData({password : res.detail.value})
     }   
     this.data.phonenumber && this.data.password && this.setData({isdisabled : false})
  },
  getCode() {
     wx_request_promisify({
        url : prefix + '/open/safetykey/getkey.json',
        method : 'GET'
     }).then(res => {
        return wx_request_promisify({
          url : prefix + '/plugins/sms/open/sendCaptcha.json',
          data : {
              "phonenumber" : this.data.phonenumber,
              "bskeyid" : res.data.data,
              "type":"1"
          },
          method : "GET"
        })
     }).then(res => {
        console.log(res)
     })
  },
  loginSubmit() {
     wx_request_promisify({
       url : prefix + '/login',
       header: {
         'content-type': 'application/x-www-form-urlencoded'
       },
       method : 'POST',
       data : {
          username : this.data.phonenumber,
          password : md5.hexMD5(this.data.password),
          login_ct : 'ajax'
       }
     }).then(res => {
      console.info(res);
      if (res.data.success) {
        wx.setStorageSync('JSESSIONID', res.data.data.JSESSIONID);
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000,
          success: function (e) {
            wx.redirectTo({
              url: '/pages/clazz/clazz'
            });
          }
        });

      } else {
        wx.showToast({
          title: '登录失败',
          icon: 'warn',
          duration: 2000
        })
      }
    }).catch(e => {
      wx.showModal({
        title: '登录失败',
        success: function (res) {
          if (res.confirm) {

          } else if (res.cancel) {

          }
        }
      })
    })
  }
})